Skip to content

Add sparse tensor primitives with einsum contraction#48

Closed
imlvts wants to merge 6 commits intotrueagi-io:mainfrom
imlvts:sparse-tensor-integration
Closed

Add sparse tensor primitives with einsum contraction#48
imlvts wants to merge 6 commits intotrueagi-io:mainfrom
imlvts:sparse-tensor-integration

Conversation

@imlvts
Copy link
Copy Markdown
Collaborator

@imlvts imlvts commented Apr 4, 2026

Integrate N-dimensional sparse tensors into MORK expressions:

  • SparseTensorF64: PathMap-backed sparse tensor with BOB encoding, arbitrary rank, element-wise add/mul via lattice operations
  • einsum-dyn: copied into workspace as local crate, runtime Einstein summation with VM compiler supporting arbitrary N-D specs
  • Tensor sinks (tensor_collect, tensor_einsum, tensor_add, tensor_mul, tensor_free) using WriteResource::TensorStore through the standard resource infrastructure — no thread-locals
  • tensor_get/tensor_nnz pure functions via ExprSource.context pointer propagated through EvalScope
  • End-to-end test: load matrix data as S-expressions, collect into tensors via exec, einsum multiply, verify result

MeTTa usage:

(exec P1 (, (a $r $c $v)) (O (tensor_collect A $r $c $v)))
(exec P2 (, (b $r $c $v)) (O (tensor_collect B $r $c $v)))
(exec P3 (,) (O (tensor_einsum "ab,bc->ac" A B C)))

imlvts and others added 6 commits April 4, 2026 16:59
Integrate N-dimensional sparse tensors into MORK expressions:

- SparseTensorF64: PathMap-backed sparse tensor with BOB encoding,
  arbitrary rank, element-wise add/mul via lattice operations
- einsum-dyn: copied into workspace as local crate, runtime Einstein
  summation with VM compiler supporting arbitrary N-D specs
- Tensor sinks (tensor_collect, tensor_einsum, tensor_add, tensor_mul,
  tensor_free) using WriteResource::TensorStore through the standard
  resource infrastructure — no thread-locals
- tensor_get/tensor_nnz pure functions via ExprSource.context pointer
  propagated through EvalScope
- End-to-end test: load matrix data as S-expressions, collect into
  tensors via exec, einsum multiply, verify result

MeTTa usage:
  (exec P1 (, (a $r $c $v)) (O (tensor_collect A $r $c $v)))
  (exec P2 (,) (O (tensor_einsum "ab,bc->ac" A B C)))

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- tensor_get now parses indices from string symbols (not binary u32),
  matching how MeTTa S-expressions encode numbers
- End-to-end test exercises full pipeline: load data → tensor_collect →
  tensor_einsum → tensor_get to verify C[0,0]=19.0 and C[1,1]=50.0
- TensorEinsumSink strips quotes from spec string
- Command sinks (einsum, binop, free) parse args in new() for literal
  templates, or in sink() for variable-bound templates

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The opaque context pointer now flows through the extern "C" call
signature rather than being set on each ExprSource before dispatch.
tensor_get/tensor_nnz read it directly from the arg; EvalScope.context
and ASink::set_context remain the owner-facing knob.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The `parsed` guard in `sink()` captured only the first pattern match and
dropped subsequent ones, so `(exec (, (to_free $n)) (O (tensor_free $n)))`
freed one tensor regardless of how many facts matched. Collect all names
across `sink()` calls and drain them in `finalize()`. The test exercises
three concurrent matches with one non-match that must survive.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Both pass — the basic TensorBinopSink path works for constant names
under an empty `(,)` pattern. Does not yet cover variable-bound names
or multi-match dispatch, which are the likelier breakage surfaces
hinted at by the todo comment.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Drop the `entries: Vec<(Vec<usize>, f64)>` buffer — parse each matched
tuple in `sink()` and call `SparseTensorF64::set` on the stored tensor
directly. The first match replaces any existing tensor at `name` with a
fresh one of the sink's rank, so repeated exec invocations start clean;
subsequent matches in the same invocation accumulate. Tracked with one
`initialized: bool` instead of the entry Vec. Safe as long as no
concurrent tensor-as-source path reads `name` during the exec.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@imlvts imlvts closed this Apr 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant